package smap;

import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Set;

import DAO.TeseDAO;
import BD.TeseBD;
import DAO.LinhaPesquisaDAO;
import BD.LinhaPesquisaBD;
import BD.DisciplinaBD;
import javax.swing.JOptionPane;

public class TelaAltTese extends javax.swing.JDialog {

    TeseBD tes = new TeseBD();
    TeseDAO tesDao = new TeseDAO();
    
    //Carregar combobox de linha de pesquisa
    LinhaPesquisaDAO lpDao = new LinhaPesquisaDAO();
    List<LinhaPesquisaBD> listalp = new ArrayList<LinhaPesquisaBD>();
    
    public TelaAltTese(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlCadastrarTese = new javax.swing.JPanel();
        lblCodigo = new javax.swing.JLabel();
        lblRa = new javax.swing.JLabel();
        txtCodigo = new javax.swing.JTextField();
        txtRa = new javax.swing.JTextField();
        lblAluno = new javax.swing.JLabel();
        txtAluno = new javax.swing.JTextField();
        lblTitulo = new javax.swing.JLabel();
        txtTitulo = new javax.swing.JTextField();
        lblLinhaPesq = new javax.swing.JLabel();
        cmbLinhaPesq = new javax.swing.JComboBox();
        btnTelaTese = new javax.swing.JButton();
        pnlVoltar3 = new javax.swing.JPanel();
        btnVoltar = new javax.swing.JButton();
        btnAlterar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Alterar tese");
        setResizable(false);

        pnlCadastrarTese.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(java.awt.Color.lightGray), "Alterar Tese"));

        lblCodigo.setText("* Código:");

        lblRa.setText("* RA:");

        txtCodigo.setEnabled(false);

        txtRa.setEnabled(false);

        lblAluno.setText("Aluno:");

        txtAluno.setEnabled(false);

        lblTitulo.setText("* Título:");

        lblLinhaPesq.setText("* Linha de Pesquisa:");

        btnTelaTese.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/area-icon.png"))); // NOI18N
        btnTelaTese.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTelaTeseActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlCadastrarTeseLayout = new javax.swing.GroupLayout(pnlCadastrarTese);
        pnlCadastrarTese.setLayout(pnlCadastrarTeseLayout);
        pnlCadastrarTeseLayout.setHorizontalGroup(
            pnlCadastrarTeseLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlCadastrarTeseLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlCadastrarTeseLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlCadastrarTeseLayout.createSequentialGroup()
                        .addComponent(lblCodigo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(lblRa)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtRa, javax.swing.GroupLayout.DEFAULT_SIZE, 193, Short.MAX_VALUE))
                    .addGroup(pnlCadastrarTeseLayout.createSequentialGroup()
                        .addComponent(lblLinhaPesq)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cmbLinhaPesq, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnTelaTese))
                    .addGroup(pnlCadastrarTeseLayout.createSequentialGroup()
                        .addGroup(pnlCadastrarTeseLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblTitulo)
                            .addComponent(lblAluno))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(pnlCadastrarTeseLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtAluno)
                            .addComponent(txtTitulo))))
                .addContainerGap())
        );
        pnlCadastrarTeseLayout.setVerticalGroup(
            pnlCadastrarTeseLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlCadastrarTeseLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlCadastrarTeseLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblCodigo)
                    .addComponent(txtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblRa)
                    .addComponent(txtRa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(pnlCadastrarTeseLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblAluno)
                    .addComponent(txtAluno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(pnlCadastrarTeseLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblTitulo)
                    .addComponent(txtTitulo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(pnlCadastrarTeseLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(pnlCadastrarTeseLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lblLinhaPesq)
                        .addComponent(cmbLinhaPesq, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(btnTelaTese))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pnlVoltar3.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(192, 192, 192)));

        btnVoltar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/voltar-icon.png"))); // NOI18N
        btnVoltar.setText("Voltar");
        btnVoltar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnVoltarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlVoltar3Layout = new javax.swing.GroupLayout(pnlVoltar3);
        pnlVoltar3.setLayout(pnlVoltar3Layout);
        pnlVoltar3Layout.setHorizontalGroup(
            pnlVoltar3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlVoltar3Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnVoltar, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(163, 163, 163))
        );
        pnlVoltar3Layout.setVerticalGroup(
            pnlVoltar3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlVoltar3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnVoltar, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        btnAlterar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/add-icon.png"))); // NOI18N
        btnAlterar.setText("Cadastrar");
        btnAlterar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAlterarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnlCadastrarTese, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(pnlVoltar3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnAlterar, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(155, 155, 155))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(pnlCadastrarTese, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnAlterar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(pnlVoltar3, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        java.awt.Dimension dialogSize = getSize();
        setLocation((screenSize.width-dialogSize.width)/2,(screenSize.height-dialogSize.height)/2);
    }// </editor-fold>//GEN-END:initComponents

    private void btnTelaTeseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTelaTeseActionPerformed
        this.setEnabled(false);
        TelaLinhaPesquisa lp = new TelaLinhaPesquisa(null, true);
        lp.carregarTabela(); //carregar tabela da tela linha de pesquisa
        lp.setVisible(true); //abrir tela linha de pesquisa
        carregarComboLinhaPesq(); //recarregar combo de linha de pesquisa
        this.setEnabled(true);
    }//GEN-LAST:event_btnTelaTeseActionPerformed

    private void btnVoltarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnVoltarActionPerformed
        this.dispose(); // Fecha Tela de Cadastro de Tese
    }//GEN-LAST:event_btnVoltarActionPerformed

    private void btnAlterarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAlterarActionPerformed
        //seta os dados para para alterar
        tes.setCodigo(Integer.parseInt(txtCodigo.getText()));
        tes.setRa_al(Integer.parseInt(txtRa.getText()));
        tes.setNomeAluno(txtAluno.getText().trim());
        tes.setTitulo(txtTitulo.getText().trim());
        
        if(cmbLinhaPesq.getSelectedIndex() == -1 || cmbLinhaPesq.getSelectedIndex() == 0){
             JOptionPane.showMessageDialog(null,"Selecione uma linha de pesquisa", 
                     "Selecione a linha de pesquisa", JOptionPane.INFORMATION_MESSAGE);
        }else
        {
            tes.setCodLinhaPesquisa(listalp.get(cmbLinhaPesq.getSelectedIndex()).getCodigo());

            if(tesDao.alterarTese(tes)){
                this.dispose();
            } 
        }

    }//GEN-LAST:event_btnAlterarActionPerformed

    public void carregarComboLinhaPesq(){
        cmbLinhaPesq.removeAllItems(); //limpar combobox
        listalp.removeAll(listalp); //limpar a lista
        listalp = lpDao.carregarComboLinhaPesquisa();
        
        for(int i = 0; i<listalp.size(); i++){
            cmbLinhaPesq.addItem(listalp.get(i));
        }  
    }
    
    public void recebeObjejo (TeseBD ts){
        txtCodigo.setText(String.valueOf(ts.getCodigo()));
        txtTitulo.setText(ts.getTitulo());
        txtAluno.setText(ts.getNomeAluno());
        txtRa.setText(String.valueOf(ts.getRa_al()));
        
        LinhaPesquisaBD lpbd = new LinhaPesquisaBD();
        lpbd.setCodigo(ts.getCodLinhaPesquisa());
        lpbd.setNomeLinhaPesquisa(ts.getLinhaPesquisa());
        
        cmbLinhaPesq.setSelectedItem(lpbd);
    }
    
    public static void main(String args[]) {

        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(TelaAltTese.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TelaAltTese.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TelaAltTese.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TelaAltTese.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                TelaAltTese dialog = new TelaAltTese(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAlterar;
    private javax.swing.JButton btnTelaTese;
    private javax.swing.JButton btnVoltar;
    private javax.swing.JComboBox cmbLinhaPesq;
    private javax.swing.JLabel lblAluno;
    private javax.swing.JLabel lblCodigo;
    private javax.swing.JLabel lblLinhaPesq;
    private javax.swing.JLabel lblRa;
    private javax.swing.JLabel lblTitulo;
    private javax.swing.JPanel pnlCadastrarTese;
    private javax.swing.JPanel pnlVoltar3;
    private javax.swing.JTextField txtAluno;
    private javax.swing.JTextField txtCodigo;
    private javax.swing.JTextField txtRa;
    private javax.swing.JTextField txtTitulo;
    // End of variables declaration//GEN-END:variables
}
